package cn.yl.order.server.controller;


import cn.yl.order.server.entity.PayOrder;
import cn.yl.order.server.entity.vo.PayOrderVo;
import cn.yl.order.server.service.PayOrderService;
import cn.yl.order.server.service.UserCourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 支付订单信息表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-04-22
 */
@RestController
@RequestMapping("/payOrder")
public class PayOrderController {
    @Autowired
    private UserCourseOrderService userCourseOrderService;

    @Autowired
    private PayOrderService payOrderService;

    @PutMapping("saveOrder")
    public void saveOrder(@RequestBody PayOrderVo payOrderVo){
        userCourseOrderService.saveOrder(payOrderVo);
    }

    @PutMapping("/{orderId}/{status}")
   public Map<String ,String > updateByStatus(@PathVariable String orderId, @PathVariable Integer status){
        PayOrder payOrder = payOrderService.updateByStatus(orderId, status);
        HashMap<String, String> map = new HashMap<>();
        map.put("courseId",payOrder.getProductId().toString());
        map.put("orderId",payOrder.getOrderNo());
        map.put("userId",payOrder.getUserId().toString());
        return map;
    }

    @GetMapping("getCurrentPrice/{orderId}")
    public BigDecimal getCurrentPrice(@PathVariable String orderId){
        BigDecimal currentPrice = this.payOrderService.getCurrentPrice(orderId);
        return currentPrice;
    }

    @PostMapping("getPayOrder/{orderId}")
    public Map<String ,String> getPayOrder(@PathVariable String orderId){
        HashMap<String, String> map = new HashMap<>();
        PayOrder payOrder = this.payOrderService.getPayOrder(orderId);
        map.put("orderId",payOrder.getOrderNo());
        map.put("status",payOrder.getStatus().toString());
        return map;
    }



}

